import http from '@/utils/http';
import type { LoginData, RegisterData, User, AuthResponse,RefreshTokenResponse } from '@/types';


// 认证相关API
export const authApi = {
  login: (data: LoginData) => http.post<AuthResponse>('/auth/login', data),
  register: (data: RegisterData) => http.post<AuthResponse>('/auth/register', data),
  logout: () => http.post('/auth/logout'),
  refreshToken: (token: string) => http.post<RefreshTokenResponse>('/auth/refresh', { token }),
  getUserInfo: () => http.get<User>('/auth/me')
};

// 商品相关API
export const productApi = {
  getProducts: (params?: any) => http.get('/products', { params }),
  getProductDetail: (id: string) => http.get(`/products/${id}`),
  searchProducts: (keyword: string) => http.get('/products/search', { params: { keyword } })
};

// 购物车相关API
export const cartApi = {
  getCart: () => http.get('/cart'),
  addToCart: (productId: string, quantity: number) => http.post('/cart', { productId, quantity }),
  updateCartItem: (itemId: string, quantity: number) => http.put(`/cart/${itemId}`, { quantity }),
  removeFromCart: (itemId: string) => http.delete(`/cart/${itemId}`),
  clearCart: () => http.delete('/cart/clear')
};

// 订单相关API
export const orderApi = {
  createOrder: (data: any) => http.post('/orders', data),
  getOrders: (params?: any) => http.get('/orders', { params }),
  getOrderDetail: (id: string) => http.get(`/orders/${id}`),
  cancelOrder: (id: string) => http.put(`/orders/${id}/cancel`)
};

// 用户相关API
export const userApi = {
  updateProfile: (data: any) => http.put('/user/profile', data),
  updatePassword: (data: any) => http.put('/user/password', data),
  getAddresses: () => http.get('/user/addresses'),
  addAddress: (data: any) => http.post('/user/addresses', data)
};